/**
 * Created by Administrator on 2017/8/7.
 */
var SEARCH_URL = "http://39.108.226.96/HSZWeb/WxYECX";
var RECHARGE_URL = "http://39.108.226.96/HSZWeb/WxYECZ";

$(function () {
    var info = getUserInfo();
    $('.search form button').on('click', function (e) {
        e.preventDefault();
        var value = $(".search form input").val();
        if (value) {
            refreshUserInfo(value);
        }

        // setTimeout(function () {
        //     $("#loading").hide();
        //     $("#result").show();
        // }, 1000);
    });

    $("#rechargeModal .modal-footer>button:nth-child(2)").on('click', function () {
        var value = $("#rechargeModal .modal-body input").val();
        if (value) {
            var userid = $(".operate>button").data("userid");
            $.ajax({
                url: RECHARGE_URL,
                type: 'post',
                data: {
                    userid: userid,
                    money: value,
                    username: info.userinfo.username,
                    token: info.userinfo.token
                },
                success: function (res) {
                    if (res.code == 200) {
                        refreshUserInfo(userid);
                    } else {
                        alert(res.data);
                    }
                },
                error: function () {
                    alert("请求超时");
                },
                complete: function () {
                    $("#rechargeModal .modal-body input").val("");
                }
            });
        }
    });

    function refreshUserInfo(userId) {
        $("#loading").show();
        $.ajax({
            url: SEARCH_URL,
            type: "post",
            data: {
                userid: userId,
                username: info.userinfo.username,
                token: info.userinfo.token
            },
            success: function (res) {
                if (res.code == 201) {
                    searchError(res.data);
                    return;
                }
                if (res.balance >= 0) {
                    // 说明用户存在
                    // <div class="user">
                    //         <h3>吴天航</h3>
                    //         <p>账户余额: <span class="badge">113￥</span></p>
                    //     </div>
                    //
                    //     <div class="operate">
                    //         <button class="btn btn_primary" data-toggle="modal" data-target="#rechargeModal">充 值</button>
                    //     </div>
                    var html = "";
                    html += '<div class="user">';
                    html += '        <h3>用户ID：' + res.userid + '</h3>';
                    html += '        <p>账户余额: <span class="badge">' + res.balance + '￥</span></p>';
                    html += '</div>';
                    html += '    <div class="operate">';
                    html += '        <button class="btn btn_primary" data-toggle="modal" data-userid="' + res.userid + '" data-target="#rechargeModal">充 值</button>';
                    html += '    </div>';
                    $("#result").html(html);
                } else {
                    // 用户不存在
                    searchError("用户不存在")
                }
            },
            error: function (e) {
                searchError("请求超时")
            },
            complete: function () {
                $("#loading").hide();
                $("#result").show();
            }
        });
    }

    function searchError(msg) {
        var html = '<h2 class="text-center text-danger">' + msg + '</h2>';
        $("#result").html(html);
        $("#loading").hide();
        $("#result").show();
    }


    function getUserInfo() {
        var url = location.href;
        var extra = url.substring(url.indexOf("?"));
        var userinfo = JSON.parse(decodeURI(url.substring(url.indexOf("userInfo=") + 9)));
        return {
            extra: extra,
            userinfo: userinfo
        }
    }
});